package com.esd.crm.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.esd.crm.Constants;
import com.esd.crm.entity.Company;
import com.esd.crm.entity.Customer;
import com.esd.crm.service.CompanyService;
import com.esd.crm.service.CustomerService;

@Controller
@RequestMapping("/security")
public class CompanyController {

	private static final Logger logger = LogManager.getLogger(CompanyController.class);

	@Autowired
	private CompanyService companyService;
	@Autowired
	private CustomerService customerService;

	/**
	 * 跳转添加订单页
	 */
	@RequestMapping(value = "/addCompany", method = RequestMethod.GET)
	public ModelAndView addCompanyGet(HttpSession session, HttpServletRequest request) {
		logger.info("跳转到添加公司地址页面");
		String customerId = request.getParameter("customerId");
		if (StringUtils.isNotBlank(customerId)) {
			Customer c = customerService.selectByPrimaryKey(Integer.valueOf(customerId));
			Company company = companyService.selectByPrimaryKey(c.getId());
			if (company != null) {
				logger.info(company);
				ModelAndView mv = new ModelAndView("company/company_update");
				mv.addObject("customerId", c.getId());
				mv.addObject("companyName", company.getCompanyName());
				mv.addObject("corporation", company.getCorporation());
				mv.addObject("taxpayerIdentificationNumber", company.getTaxpayerIdentificationNumber());
				mv.addObject("companyAddress", company.getCompanyAddress());
				mv.addObject("companyTelephone", company.getCompanyTelephone());
				mv.addObject("accountsBank", company.getAccountsBank());
				mv.addObject("bankNumber", company.getBankNumber());
				return mv;
			} else {
				ModelAndView mv = new ModelAndView("company/company_add");
				mv.addObject("customerId", c.getId());
				mv.addObject("company", c.getCompany());
				return mv;
			}

		}
		return null;
	}

	/**
	 * 跳转添加订单页
	 */
	@RequestMapping(value = "/addCompany", method = RequestMethod.POST)
	public ModelAndView addCompanyPost(HttpSession session, HttpServletRequest request,
			RedirectAttributes redirectAttributes) {
		logger.info("添加订单页");
		Object userId = session.getAttribute(Constants.USER_ID);
		Integer createuser = Integer.valueOf(userId.toString());
		String customerid = request.getParameter("customerId");
		if (StringUtils.isBlank(customerid)) {

		}
		String shi = request.getParameter("companyName");
		String companyName = request.getParameter("companyName");
		String companyAddress = request.getParameter("companyAddress");
		String companyTelephone = request.getParameter("companyTelephone");
		String accountsBank = request.getParameter("accountsBank");
		String bankNumber = request.getParameter("bankNumber");
		String corporation = request.getParameter("corporation");
		String taxpayerIdentificationNumber = request.getParameter("taxpayerIdentificationNumber");

		Company c = new Company();
		c.setId(Integer.valueOf(customerid));
		c.setCompanyName(companyName);
		c.setCompanyAddress(companyAddress);
		c.setCompanyTelephone(companyTelephone);
		c.setAccountsBank(accountsBank);
		c.setBankNumber(bankNumber);
		c.setCorporation(corporation);
		c.setTaxpayerIdentificationNumber(taxpayerIdentificationNumber);
		c.setCreateuser(createuser);

		companyService.insertSelective(c);

		redirectAttributes.addAttribute("customerId", customerid);
		return new ModelAndView("redirect:/security/successAndClose");
	}
	
	/**
	 * 跳转添加订单页
	 */
	@RequestMapping(value = "/updateCompany", method = RequestMethod.POST)
	public ModelAndView updateCompanyPost(HttpSession session, HttpServletRequest request,
			RedirectAttributes redirectAttributes) {
		logger.info("添加订单页");
		Object userId = session.getAttribute(Constants.USER_ID);
		Integer createuser = Integer.valueOf(userId.toString());
		String customerid = request.getParameter("customerId");
		if (StringUtils.isBlank(customerid)) {

		}
		String shi = request.getParameter("companyName");
		String companyName = request.getParameter("companyName");
		String companyAddress = request.getParameter("companyAddress");
		String companyTelephone = request.getParameter("companyTelephone");
		String accountsBank = request.getParameter("accountsBank");
		String bankNumber = request.getParameter("bankNumber");
		String corporation = request.getParameter("corporation");
		String taxpayerIdentificationNumber = request.getParameter("taxpayerIdentificationNumber");

		Company c = new Company();
		c.setId(Integer.valueOf(customerid));
		c.setCompanyName(companyName);
		c.setCompanyAddress(companyAddress);
		c.setCompanyTelephone(companyTelephone);
		c.setAccountsBank(accountsBank);
		c.setBankNumber(bankNumber);
		c.setCorporation(corporation);
		c.setTaxpayerIdentificationNumber(taxpayerIdentificationNumber);
		c.setCreateuser(createuser);

		companyService.updateByPrimaryKeySelective(c);

		redirectAttributes.addAttribute("customerId", customerid);
		return new ModelAndView("redirect:/security/successAndClose");
	}
}
